package com.zhiyou.crud.service.impl;

import com.zhiyou.crud.bean.*;
import com.zhiyou.crud.dao.*;
import com.zhiyou.crud.service.ProjectService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 王冲冲
 * @version 1.0
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Resource
    ProjectMapper projectMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    TypeMapper typeMapper;
    @Resource
    RealMapper realMapper;
    @Resource
    CommentMapper commentMapper;
    @Resource
    DonateMapper donateMapper;

    //获取所有项目
    @Override
    public List<Project> getAllProject() {
        List<Project> projects = projectMapper.selectAll();
        for (int i = 0; i < projects.size(); i++) {
            Example example = new Example(User.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("uno", projects.get(i).getUno());
            List<User> users = userMapper.selectByExample(example);
            projects.get(i).setUser(users.get(0));

            Example example1 = new Example(Type.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("tno", projects.get(i).getTno());
            List<Type> types = typeMapper.selectByExample(example1);
            projects.get(i).setTname(types.get(0).getTname());
        }
        return projects;
    }
    //搜索项目
    @Override
    public List<Project> getProject(Project project) {
        System.out.println("搜索项目");
        System.out.println(project.getUname());
        //定义一个集合
        List<Project> projects = new ArrayList<>();
        //首先查询一个名字对应了多少个uno;
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uname", project.getUname());
        List<User> users = userMapper.selectByExample(example);
        //定义一个集合存储1个或多个uno(重名)
        List list = new ArrayList();
        for (int i = 0; i < users.size(); i++) {
            list.add(users.get(i).getUno());
            System.out.println(users.get(i).getUno());
        }
        //通过名字查询到tno;
        Example example1 = new Example(Type.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("tname", project.getTname());
        List<Type> types = typeMapper.selectByExample(example1);
        Integer tno = types.get(0).getTno();

        for (int i = 0; i < list.size(); i++) {
            Example example2 = new Example(Project.class);
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.andEqualTo("uno", list.get(i)).andEqualTo("ptitle", project.getPtitle())
                    .andEqualTo("pstatus", project.getPstatus()).andEqualTo("tno", tno);
            List<Project> projects1 = projectMapper.selectByExample(example2);
            projects.add(projects1.get(0));
        }
        //将需要的属性加入到里面
        for (int i = 0; i < projects.size(); i++) {
            projects.get(i).setUname(users.get(i).getUname());
            projects.get(i).setTname(types.get(0).getTname());
        }
        return projects;
    }
    //查看所有信息
    @Override
    public Project getPerByPno(Integer pno) {
        Project project = projectMapper.selectByPrimaryKey(pno);
        Example example = new Example(Real.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uno", project.getUno());
        System.out.println("==============================================");
        System.out.println(project.getUno());
        List<Real> reals = realMapper.selectByExample(example);
        System.out.println(reals.get(0));
        project.setReal(reals.get(0));
        Type type = typeMapper.selectByPrimaryKey(project.getTno());
        project.setTname(type.getTname());

        Example example1 = new Example(Comment.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("pno", pno);
        List<Comment> comments = commentMapper.selectByExample(example1);
        project.setComments(comments);

        Example example2 = new Example(Donate.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria2.andEqualTo("pno", pno);
        List<Donate> donates = donateMapper.selectByExample(example2);


        for (int i = 0; i < donates.size(); i++) {
            User user = userMapper.selectByPrimaryKey(donates.get(i).getUno());
            donates.get(i).setUser(user);
        }

        for (int i = 0; i < comments.size(); i++) {
            User user = userMapper.selectByPrimaryKey(comments.get(i).getUno());
            comments.get(i).setUser(user);
        }
        project.setDonates(donates);
        return project;
    }

    //查看项目信息
    @Override
    public Project getProByPno(Integer pno) {
        Project project = projectMapper.selectByPrimaryKey(pno);
        System.out.println(project.getTno());
        Type type = typeMapper.selectByPrimaryKey(project.getTno());
        project.setTname(type.getTname());
        System.out.println(project);
        return project;
    }

    @Override
    public List<Comment> getCommentByPno(Integer pno) {
        Project project = projectMapper.selectByPrimaryKey(pno);
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pno", pno);
        List<Comment> comments = commentMapper.selectByExample(example);
        return comments;
    }

    @Override
    public Project getJinDu(Integer pno) {
        Project project = projectMapper.selectByPrimaryKey(pno);
        Example example = new Example(Donate.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pno", pno);
        List<Donate> donates = donateMapper.selectByExample(example);
        project.setDonates(donates);
        return project;
    }


    //查看所有未审核的项目
    @Override
    public List<Project> getAllNoPro() {
        Example example = new Example(Project.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pstatus", 1);
        List<Project> projects = projectMapper.selectByExample(example);

        for (int i = 0; i < projects.size(); i++) {
            Example example1 = new Example(User.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("uno", projects.get(i).getUno());
            List<User> users = userMapper.selectByExample(example1);
            projects.get(i).setUser(users.get(0));

            Example example2 = new Example(Type.class);
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.andEqualTo("tno", projects.get(i).getTno());
            List<Type> types = typeMapper.selectByExample(example2);
            projects.get(i).setTname(types.get(0).getTname());
        }


        return projects;
    }

    //通过项目
    @Override
    public int passProject(Project project) {
        System.out.println("============================================================");
        project.setPstatus(3);
        return projectMapper.updateByPrimaryKeySelective(project);
    }

    @Override
    public int failProject(Project project) {
        Example example = new Example(Project.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pno", project.getPno()).andEqualTo("pstatus", 2);
        return projectMapper.updateByPrimaryKeySelective(project);
    }

    //查看所有的评论
    @Override
    public List<Comment> getAllComment() {
        List<Comment> comments = commentMapper.selectAll();
//给每个评论找到评论的项目和评论的人
        for (int i = 0; i < comments.size(); i++) {
            //找到项目名字
            Project project = projectMapper.selectByPrimaryKey(comments.get(i).getPno());
            comments.get(i).setProject(project);
            //找到评论人名
            User user = userMapper.selectByPrimaryKey(comments.get(i).getUno());
            comments.get(i).setUser(user);
        }
        return comments;
    }

    //删除一个评论
    @Override
    public int deleteOneComment(Integer cono) {
        int i = commentMapper.deleteByPrimaryKey(cono);
        return i;
    }


    //新增一个类型
    @Override
    public int addOneType(Type type) {
        System.out.println("0000000000000000000000000000000000000000000");
        return typeMapper.insert(type);

    }

    //删除一个类型
    @Override
    public int deleteOneType(Integer tno) {
        int i = typeMapper.deleteByPrimaryKey(tno);
        return i;
    }

    @Override
    public List<Type> getAllType() {
        List<Type> types = typeMapper.selectAll();
        return types;
    }

    @Override
    public int updateOneType(Type type) {
        return typeMapper.updateByPrimaryKeySelective(type);
    }

}

